﻿namespace LogManager.Core.Models
{
    using LogManager.Core.Helpers;
    using LogManager.Utils.Unity;
    using LogManager.Utils.Xml;
    using System;
    using System.Collections.ObjectModel;
    using System.Collections.Specialized;
    using System.Linq;
    using System.Xml.Linq;
    using LogManager.Core.Factories;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    [Serializable]
    public class Plateform : XmlElementBase, IPlateform
    {
        public const string DEFAULT_TEMPLATE = @"<Plateform Name=""Plateform name"" Domain=""domain"" Depository=""d$\Logs\MyLogsDirectory"" Login=""login"" Password=""passWord"">" + Server.DEFAULT_TEMPLATE + "</Plateform>";

        public virtual string Name { get; set; }
        public virtual string Domain { get; set; }
        public virtual string Login { get; set; }
        public virtual string Password { get; set; }
        public virtual string Depository { get; set; }
        public virtual IWarehouse ParentWarehouse { get; protected set; }
        public ObservableCollection<IServer> Servers { get; protected set; }

        public readonly IXmlElementFactory _xmlElementFactory;

        public Plateform(XElement baseNode, IWarehouse parentWarehouse, IXmlElementFactory xmlElementFactory)
            : base(baseNode)
        {
            this._xmlElementFactory = xmlElementFactory;
            this.ParentWarehouse = parentWarehouse;
            this.Initialize();
            this.PropertyChanged += (a, b) => SaveChanges();
            this.Servers.CollectionChanged += (a, b) => SaveChanges();
        }

        private void Initialize()
        {
            var xml = new XmlResolver<Plateform>(BaseNode);
            this.Name = xml.GetAttributeValue(c => c.Name);
            this.Domain = xml.GetAttributeValue(c => c.Domain);
            this.Login = xml.GetAttributeValue(c => c.Login);
            this.Password = xml.GetAttributeValue(c => c.Password);
            this.Depository = xml.GetAttributeValue(c => c.Depository);
            this.Servers = new ObservableCollection<IServer>(xml.ResolveChildren(c => c.Servers, xElm => this._xmlElementFactory.CreateServer(xElm, this)));
        }

        public override void SaveChanges()
        {
            var xml = new XmlResolver<Plateform>(BaseNode);
            xml.SetAttributeValue(c => c.Name, Name);
            xml.SetAttributeValue(c => c.Domain, Domain);
            xml.SetAttributeValue(c => c.Login, Login);
            xml.SetAttributeValue(c => c.Password, Password);
            xml.SetAttributeValue(c => c.Depository, Depository);
            xml.SetChildren(c => c.Servers, Servers.OfType<IXmlElement>());
        }

        public void DeletePlateform()
        {
            this.ParentWarehouse.Plateforms.Remove(this);
        }

        public IServer AddNewServer()
        {
            var instance = this._xmlElementFactory.CreateNewServer(this);
            this.Servers.Add(instance);
            return instance;
        }
    }
}
